﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common.Model;
using pinduoduo;
using pinduoduo.Model;
using Tk.Soc.Share.Common.Helpers;

namespace Common.Service
{
    public class PddAddressService
    {
        /// <summary>
        /// 地址转换
        /// </summary>
        /// <returns></returns>
        public async Task<string> AddressToId(PddToken pddToken,string daili,string address) 
        {
            string[] addressArr = address.Split(' ');
            if (addressArr.Length != 4)
            {
                return "";
            }

            var res = await pinduoduo.PddApi.GetRegions(pddToken,1);
            if (res.StatusCode == System.Net.HttpStatusCode.OK) 
            {
               var regions = JsonHelper.DeserializeObject<Regions>(res.Content);
                RegionsItem curRegionsItem =null;
                foreach (var regionItem in regions.regions)
                {
                    if (regionItem.region_name == addressArr[0]) 
                    {
                        curRegionsItem = regionItem;
                        break;
                    }
                }

                if (curRegionsItem == null) 
                {
                    return null;
                }

                res = await pinduoduo.PddApi.GetRegions(pddToken,  curRegionsItem.region_id);

                foreach (var regionItem in regions.regions)
                {
                    if (regionItem.region_name == addressArr[0])
                    {
                        curRegionsItem = regionItem;
                        break;
                    }
                }

                if (curRegionsItem == null)
                {
                    return null;
                }
            }

            return null;
        }
    }
}
